let host= 'cn-shanghai.log.aliyuncs.com'
let project = 'nice-monitor'
let logStore = 'nice-monitor'
let getUserAgent = require('user-agent')

function getExtraData () {
  return {
    title: document.title,
    url: location.href,
    timestamp: Date.now()+'',
    userAgent:getUserAgent.parse(navigator.userAgent).name
  }
}
class SendTracker{
  constructor() {
    this.url = `https://${project}.${host}/logstores/${logStore}/track`;//上报的路径
    this.xhr = new XMLHttpRequest();
  }
  send (data = {}) {
    let extraData = getExtraData()
    this.xhr.open('POST', this.url, true)
    const logs = { __source__: 'error', __logs__: [{ ...data, ...extraData }] }
    let body = JSON.stringify(logs)
    this.xhr.setRequestHeader('Content-type', 'application/json')
    this.xhr.setRequestHeader('x-log-apiversion', '0.6.0')
    this.xhr.setRequestHeader('x-log-bodyrawsize', body.length)
    this.xhr.onload = function (res) {
      console.log(res);
    }
    this.xhr.onerror = function (err) {
      console.log(err);
    }
    this.xhr.send(body)
  }
}
export default new SendTracker();
